package com.crud.generator.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 代码预览集合。
 */
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CodePreviewBundle {

    @Builder.Default
    private List<CodeSegment> segments = new ArrayList<>();

    public Optional<CodeSegment> findSegment(CodeSegmentType type) {
        if (segments == null || segments.isEmpty()) {
            return Optional.empty();
        }
        return segments.stream()
            .filter(segment -> segment.getType() == type)
            .findFirst();
    }

    public boolean hasSegment(CodeSegmentType type) {
        return findSegment(type).isPresent();
    }

    public List<CodeSegment> getSegments() {
        if (segments == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(segments);
    }
}
